package com.rocket.util;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;

/**
 * 画板
 * 
 */
public class ParabolaUtil {

	Bitmap		mDrawBitmap;
	Canvas		mCanvas;
	Paint		mPen;
	//	Paint mTextPen;
	int			mPenSize	= 5;
	int			mPenColor;
	int[]		mPenPixles;

	float		mX, mY;
	Path		mPath;

	float		x1, y1, x2, y2, x3, y3;

	DrawPoint[]	points		= null;

	public ParabolaUtil(Context context) {
		init();
	}

	private void init() {
		mPen = new Paint();
		mPen.setAntiAlias(true);
		mPen.setDither(true);
		mPen.setStyle(Paint.Style.STROKE);
		mPen.setStrokeJoin(Paint.Join.ROUND);
		mPen.setStrokeCap(Paint.Cap.ROUND);
		mPen.setStrokeWidth(mPenSize);
		mPen.setColor(Color.BLACK);

		//		mTextPen = new Paint();
		//		mTextPen.setAntiAlias(true);
		//		mTextPen.setDither(true);
		//		mTextPen.setStyle(Paint.Style.FILL);
		//		mTextPen.setStrokeJoin(Paint.Join.ROUND);
		//		mTextPen.setStrokeCap(Paint.Cap.ROUND);
		//		mTextPen.setStrokeWidth(2);
		//		mTextPen.setTextSize(40);
		//		mTextPen.setTextScaleX(-1);
		//		mTextPen.setColor(Color.BLACK);

		points = new DrawPoint[3];
		points[0] = new DrawPoint();
		points[1] = new DrawPoint();
		points[2] = new DrawPoint();

		mPath = new Path();

		mDrawBitmap = Bitmap.createBitmap(550, 500, Bitmap.Config.ARGB_4444);
		mCanvas = new Canvas(mDrawBitmap);
	}

	public void drawParobola(float x1, float y1, float x2, float y2, float x3, float y3) {
		DrawPoint controlPoint = getControlPoints(x1, y1, x2, y2, x3, y3);

		points[0].x = mX = x1;
		points[0].y = mY = y1;
		points[1].x = controlPoint.x;
		points[1].y = controlPoint.y;
		points[2].x = x3;
		points[2].y = y3;

		for (float t = (float) 0.0; t <= 1.0; t += 0.01) {
			DrawPoint pd = getDrawPoint(t);
			Log.i("lijinzhe", "pd.x:" + pd.x + "   pd.y:" + pd.y);
			//			if(drawPosintX != null && drawPosintX.size() > 0){
			//				for(Integer x :drawPosintX){
			//					if(pd.x>=x){
			//						mCanvas.drawCircle(pd.x, pd.y,5, mPen);
			//						mCanvas.save(Canvas.ALL_SAVE_FLAG);
			//						drawPosintX.remove(x);
			//					}
			//				}
			//			}

			mPath.quadTo(mX, mY, pd.x, pd.y);
			mCanvas.drawPath(mPath, mPen);
			mCanvas.save(Canvas.ALL_SAVE_FLAG);
			mX = pd.x;
			mY = pd.y;
		}
	}

	public void drawParobola(float x1, float y1, float x2, float y2, float x3, float y3, ArrayList<DrawPoint> drawPosints) {

		ArrayList<DrawPoint> dps = new ArrayList<DrawPoint>(drawPosints);

		DrawPoint controlPoint = getControlPoints(x1, y1, x2, y2, x3, y3);

		points[0].x = mX = x1;
		points[0].y = mY = y1;
		points[1].x = controlPoint.x;
		points[1].y = controlPoint.y;
		points[2].x = x3;
		points[2].y = y3;

		for (float t = (float) 0.0; t <= 1.0; t += 0.01) {
			DrawPoint pd = getDrawPoint(t);
			if (dps != null && dps.size() > 0) {
				DrawPoint dp = null;
				for (DrawPoint point : dps) {
					if (pd.x >= point.x) {
						mCanvas.drawCircle(pd.x, pd.y, 5, mPen);
						mCanvas.save(Canvas.ALL_SAVE_FLAG);
						dp = point;
						break;
					}
				}
				if(dp != null){
					dps.remove(dp);
					dp = null;
				}
			}

			mPath.quadTo(mX, mY, pd.x, pd.y);
			mCanvas.drawPath(mPath, mPen);
			mCanvas.save(Canvas.ALL_SAVE_FLAG);
			mX = pd.x;
			mY = pd.y;
		}
	}

	public void drawPoint(ArrayList<DrawPoint> drawPosints) {
		if (drawPosints != null && drawPosints.size() > 0) {
			for (DrawPoint p : drawPosints) {
				mCanvas.drawCircle(p.x, p.y, 5, mPen);
				mCanvas.save(Canvas.ALL_SAVE_FLAG);
			}
		}
	}

	/**
	 * 画图点
	 * 
	 */
	public static class DrawPoint {

		public float	x;
		public float	y;

		public DrawPoint() {

		}

		public DrawPoint(float x, float y) {
			this.x = x;
			this.y = y;
		}
	}

	/**
	 * 取得画图点
	 * 
	 */
	public DrawPoint getDrawPoint(float t) {
		DrawPoint result = new DrawPoint();
		float ax, bx;
		float ay, by;
		float tSquared;

		bx = 2 * (points[1].x - points[0].x);
		ax = points[2].x + points[0].x - 2 * points[1].x;

		by = 2 * (points[1].y - points[0].y);
		ay = points[2].y + points[0].y - 2 * points[1].y;

		tSquared = t * t;

		result.x = (ax * tSquared) + (bx * t) + points[0].x;
		result.y = (ay * tSquared) + (by * t) + points[0].y;
		return result;
	}

	public DrawPoint getControlPoints(float x0, float y0, float x1, float y1, float x2, float y2) {
		DrawPoint result = new DrawPoint();

		result.x = 2 * x1 - 0.5f * x0 - 0.5f * x2;
		result.y = 2 * y1 - 0.5f * y0 - 0.5f * y2;

		return result;
	}

	public Bitmap getmDrawBitmap() {
		Matrix m = new Matrix();
		m.setScale(1, -1);
		Bitmap bitmap = Bitmap.createBitmap(mDrawBitmap, 0, 0, mDrawBitmap.getWidth(), mDrawBitmap.getHeight(), m, true);
		mDrawBitmap.recycle();
		Log.i("lijinzhe", "getmDrawBitmap: Width:" + bitmap.getWidth() + "  Height:" + bitmap.getHeight());
		return bitmap;
	}
}
